package jinyilw.swing.visuals.Icomp;

import jinyilw.swing.SwingTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.i18n.LanguageChanger;
import jinyilw.swing.visuals.text.MultiLineLabel;

import javax.swing.Icon;
import java.awt.Font;

public class IWrapLabel extends MultiLineLabel implements LanguageChanger
{
	private static final long serialVersionUID = 6453401174772004176L;
	private int wrapWidth = 200;
	private String longText;
	protected ICSetting i18nSetting;

	public IWrapLabel()
	{
	}

	public IWrapLabel(String text)
	{
		longText = text;
		updateWrapText();
	}

	public IWrapLabel(String longText, int wrapWidth)
	{
		this.longText = longText;
		this.wrapWidth = wrapWidth;
		updateWrapText();
	}

	public IWrapLabel(String longText, int wrapWidth, Font font)
	{
		this.longText = longText;
		this.wrapWidth = wrapWidth;
		setFont(font);
	}

	public IWrapLabel(ICSetting iSetting)
	{
		setText(iSetting);
	}

	public IWrapLabel(ICSetting iSetting, int wrapWidth)
	{
		setText(iSetting);
		this.wrapWidth = wrapWidth;
	}

	public IWrapLabel(ICSetting iSetting, int wrapWidth, Font font)
	{
		setText(iSetting);
		this.wrapWidth = wrapWidth;
		super.setFont(font);
	}

	public IWrapLabel(ICSetting iSetting, Icon icon, int horizontalAlignment)
	{
		super(icon, horizontalAlignment);
		setText(iSetting);
	}

	@Override
	public void setText(String longText)
	{
		if (this.longText != longText && longText != "")
		{
			this.longText = longText;
			updateWrapText();
		}
	}

	@Override
	public String getText()
	{
		return longText;
	}

	public void setWrapWidth(int width)
	{
		if (wrapWidth != width)
		{
			wrapWidth = width;
			updateWrapText();
		}
	}

	@Override
	public void setFont(Font font)
	{
		if (font == null || font.equals(getFont()))
			return;
		if (longText != null)
			texts = SwingTools
					.getChWrapTexts(longText, getFontMetrics(font), wrapWidth)
					.toArray(new String[1]);
		super.setFont(font);
	}

	private void updateWrapText()
	{
		if (longText != null && getFont() != null)
		{
			setTexts(SwingTools.getChWrapTexts(longText, this, wrapWidth)
					.toArray(new String[1]));
		} else
			setTexts(null);
	}

	public void setICSetting(ICSetting iSetting)
	{
		i18nSetting = iSetting;
	}

	@Override
	public ICSetting getICSetting()
	{
		return i18nSetting;
	}

	@Override
	public void updateText(String text)
	{
		setText(text);
	}
}
